package com.mrtan.controller;

import cn.dev33.satoken.util.SaResult;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.mrtan.bean.Supplier;
import com.mrtan.bean.SupplierProduct;
import com.mrtan.dto.MCDto;
import com.mrtan.dto.PageBeanDto;
import com.mrtan.service.SupplierService;
import com.mrtan.vo.*;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * 供应商
 * @author 李聪
 * @version 2023/12/8
import com.mrtan.mapper.SupplierMapper;
import com.mrtan.service.SupplierService;
import com.mrtan.vo.SupplierVO;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

/**
 * @author 韦威
 * @version 2023.1.3
 * @Date 2023/12/9 15:56
 */
@RestController
@RequestMapping("/supplier")
public class SupplierController {
    @Resource
    private SupplierService supplierService;
    //添加供应商
    @GetMapping("/add")
    public SaResult addClass(SupplierVO supplierVO) {
        int i = supplierService.addSupplier(supplierVO);
        if (i > 0) {
            return SaResult.ok("添加成功");
        }
        return SaResult.error("添加失败");
    }
    /**
     * 所有供应商产品
     * @param supplierProductVO
     * @return {@link SaResult}
     */
    @PostMapping("/allproduct")
    public SaResult allproduct(@RequestBody SupplierProductVO supplierProductVO){
        PageHelper.startPage(supplierProductVO.getPage(), supplierProductVO.getLimit());
        List<SupplierProduct> supplierProducts = supplierService.selectAll(supplierProductVO);
        PageInfo<SupplierProduct> pageInfo = new PageInfo<>(supplierProducts);
        PageBeanDto<SupplierProduct> pageBeanDto = new PageBeanDto();
        pageBeanDto.setTotal((int) pageInfo.getTotal());
        pageBeanDto.setList(pageInfo.getList());
        return SaResult.ok().setData(pageBeanDto);
    }

    /**
     * 所有供应商
     * @return {@link SaResult}
     */
    @PostMapping("/allsupplier")
    public SaResult allsupplier(){
        List<Supplier> suppliers = supplierService.selectAllSupplier();
        return SaResult.ok().setData(suppliers);
    }
    @PostMapping("/allBySupplierVO")
    public SaResult allBySupplierVO(@RequestBody SupplierVO supplierVO){
        PageHelper.startPage(supplierVO.getPage(),supplierVO.getLimit());
        List<Supplier> supplierList = supplierService.findAllSupplier(supplierVO);
        PageInfo<Supplier> pageInfo = new PageInfo<>(supplierList);
        PageBeanDto pageBeanDto = new PageBeanDto<>();
        pageBeanDto.setTotal((int) pageInfo.getTotal());
        pageBeanDto.setList(pageInfo.getList());
        return SaResult.ok().setData(pageBeanDto);
    }

    @PostMapping("addproduct")
    public SaResult addproduct(@RequestBody AddSupplierProductVO addSupplierProductVO){

        System.out.println(addSupplierProductVO);
        int i = supplierService.addProduct(addSupplierProductVO);

        if(i<=0){
            return SaResult.error("请检查你所填写的信息");
        }
        return SaResult.ok();
    }


    @DeleteMapping("remove/{productid}/{supplierid}")
    public SaResult addproduct(@PathVariable int productid, @PathVariable int supplierid){
        int i = supplierService.romove(productid,supplierid);
        if(i<=0){
            return SaResult.error("亲，异常操作，请稍后再试");
        }
        return SaResult.ok();
    }

    @PostMapping("update")
    public SaResult updateProduct(@RequestBody UpdateSupplierProduct
                                              updateSupplierProduct){

        int i = supplierService.updateproduct(updateSupplierProduct);
        if(i<=0){
            return SaResult.error("亲，请检查你所输入的信息");
        }
        return SaResult.ok();
    }
    @PostMapping("/updateQuantity")
    public SaResult updateQuantity(@RequestBody PurchaseOrderVO purchaseOrderVO) {
        supplierService.updateQuantity(purchaseOrderVO);
        return SaResult.ok("产品数量修改成功");
    }
    @GetMapping("/supplierCount")
    public SaResult getSupplierCount() {
        int i = supplierService.getSupplierCount();
        return SaResult.ok()
                .setData(i);
    }
    @GetMapping("/getModelAndCount/{id}")
    public SaResult getModelAndCount(@PathVariable Integer id) {
        List<MCDto> list = supplierService.getModelAndCount(id);
        return SaResult.ok()
                .setData(list);
    }
}
